package com.study.talk.module.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.study.talk.common.resullt.FwResult;
import com.study.talk.common.util.SecurityUtil;
import com.study.talk.module.entity.GroupInfo;
import com.study.talk.module.entity.vo.GroupInfoVo;
import com.study.talk.module.service.GroupInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author thor
 * @since 2023-12-18
 */
@RestController
@RequestMapping("/groupInfo")
public class GroupInfoController {


    @Resource
    private GroupInfoService groupInfoService;


    @ApiOperation("根据群聊Id获取群聊信息")
    @GetMapping("/{id}")
    public FwResult<GroupInfo> getGroupInfo(@PathVariable("id") Integer id) {
        GroupInfo groupInfo = groupInfoService.getById(id);
        if (ObjectUtils.isNotNull(groupInfo)) {
            return FwResult.ok(groupInfo);
        } else {
            return FwResult.failed();
        }
    }

    @ApiOperation("新建一个群聊")
    @PostMapping("/add")
    public FwResult<String> addGroup(@RequestBody GroupInfoVo groupInfoVo) {
        groupInfoService.save(new GroupInfo(groupInfoVo));
        return FwResult.okMsg("新建群聊成功");
    }


    @ApiOperation("根据群聊名称模糊搜索群聊")
    @GetMapping("/get/list/{groupName}")
    public FwResult<List<GroupInfo>> getGroupList(@PathVariable("groupName") String groupName) {
        return FwResult.ok(groupInfoService.getGroupListByGroupName(groupName));
    }

    @ApiOperation("根据用户id获取其加入的所有群聊")
    @GetMapping("/get/group/list/user/id/{userId}")
    public FwResult<List<GroupInfo>> getGroupListByUserId(@PathVariable("userId") Integer userId) {
        return FwResult.ok(groupInfoService.getGroupListByUserId(userId));
    }

    @ApiOperation("根据用户id获取其加入的所有群聊")
    @GetMapping("/get/group/list/user/id")
    public FwResult<List<GroupInfo>> getGroupList() {
        return FwResult.ok(groupInfoService.getGroupListByUserId(SecurityUtil.getUser().getId()));
    }

    @ApiOperation("解散群聊")
    @GetMapping("/update/disband/{groupId}")
    public FwResult<Boolean> disbandGroup(@PathVariable("groupId") Integer groupId) {
        return groupInfoService.disband(SecurityUtil.getUser().getId(), groupId);
    }

}
